﻿#include "Curve2DKeySelcetBar.h"
#include "DataChosenListDialog.h"
#include <QModelIndexList>

namespace pst
{

    Curve2DKeySelcetBar::Curve2DKeySelcetBar(QWidget* parent)
        : ElaScrollArea(parent)
        , m_dataChosenListDialog(nullptr)
        , m_text(nullptr)
        , m_lineEdit(nullptr)
        , m_button(nullptr)
        , m_selectedIndexs()
    {
        this->setObjectName("Curve2DKeySelcetBar");
        this->setGeometry(0, 0, 100, 100);
        auto layout = new QHBoxLayout(this);
        m_text = new ElaText(this);
        m_text->setTextPixelSize(14);
        m_lineEdit = new ElaLineEdit(this);
        //m_lineEdit->setText(" ");
        m_lineEdit->setEnabled(false);
        m_button = new ElaPushButton("...");
        layout->addWidget(m_text);
        layout->addWidget(m_lineEdit);
        //layout->addStretch();
        layout->addWidget(m_button);

        connect(m_button, &ElaPushButton::clicked, this,
            &Curve2DKeySelcetBar::slot_selectDataButtonClicked);
    }

    void Curve2DKeySelcetBar::setData(
        const QString& keyName, const QList<double>& values)
    {
        m_values.clear();
        m_text->setText(keyName);
        for (const auto& v : values)
        {
            m_values.append(QString::number(v));
        }
        if (!m_values.isEmpty())//默认选择第一个
        {
            m_selectedIndexs.clear();
            m_selectedIndexs << 0;
            m_lineEdit->setText(m_values[0]);
        }
    }

    void Curve2DKeySelcetBar::setData(
        const QString& keyName, const QList<QString>& values)
    {
        m_text->setText(keyName);
        m_values = values;
        if (!m_values.isEmpty())//默认选择第一个
        {
            m_selectedIndexs.clear();
            m_selectedIndexs << 0;
            m_lineEdit->setText(m_values[0]);
        }
    }

    QList<int> Curve2DKeySelcetBar::getSelectedIndex()
    {
        return m_selectedIndexs;
    }

    void Curve2DKeySelcetBar::slot_sentChosenDataIds(
        const QModelIndexList& chosenID)
    {
        m_selectedIndexs.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + ",";

                auto index = chosenID.at(i).row();
                m_selectedIndexs.append(index);
            }
        }
        m_lineEdit->setText(strText);
    }

    void Curve2DKeySelcetBar::slot_selectDataButtonClicked()
    {
        if (m_dataChosenListDialog == nullptr)
        {
            m_dataChosenListDialog = new DataChosenListDialog();
            m_dataChosenListDialog->setFixedSize(400, 400);
            m_dataChosenListDialog->moveToCenter();
            m_dataChosenListDialog->setData(m_values);
            m_dataChosenListDialog->show();
            m_dataChosenListDialog->updateUI();
            m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs);//默认选中第一个

            connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &Curve2DKeySelcetBar::slot_sentChosenDataIds);
        }
        else
        {
            m_dataChosenListDialog->show();
        }
    }

}
